package Java7_Concurrency_Cookbook.thread_executor_framework.invoke_any;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 普通类, 随机休眠, 随机返回true or false
 * @author lixiangke
 * @date 2017/11/17
 */
public class UserValidator {
    private String name;

    public UserValidator(String name) {
        this.name = name;
    }

    public boolean validate(String name, String password) {
        Random random = new Random();
        try {
            long duration = (long) random.nextInt(10);
            System.out.printf("Validator %s: Validating a user during %d seconds\n", this.name, duration);
            TimeUnit.SECONDS.sleep(duration);
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
        return random.nextBoolean();
    }

    public String getName() {
        return name;
    }

}
